<?php
/**
 * 角色控制器
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/10
 * Time: 18:28
 */
namespace app\index\controller;

use app\index\model\Privilege;
use think\Db;
use think\Session;
use think\Validate;

class Role extends Base
{
    /**
     * 角色列表页展示
     * @return mixed
     */
    public function index()
    {
        $data = \app\index\model\Role::where(["id"=>["neq",1]])->paginate(10);
        $this->assign([
            "data" => $data
        ]);
        return $this->fetch();
    }

    /**
     * 添加角色页面展示
     * @return mixed
     */
    public function addview()
    {

        $role = Session::get("role");
        $privilege_value = Db::name("role")->where(['id'=>$role])->value("privilege_list");
        if($privilege_value!="*"){
            $privilege_value = explode(",",$privilege_value);
        }


        if(empty($_GET['id'])){
            //获取一级权限组
            $where['parent_id'] = 0;
            $where['status'] = 1;

            if($privilege_value!="*") {
                $where['id'] = ['in', $privilege_value];
            }

            $privilege = Db::name("privilege")->where($where)->select();

            //查询二级
            foreach ($privilege as $k=>$v){
                $where1['parent_id'] = $v['id'];
                $where1['status'] = 1;

                if($privilege_value!="*") {
                    $where1['id'] = ['in', $privilege_value];
                }

                $click = Db::name("privilege")->where($where1)->select();

                foreach ($click as $k1=>$v1){
                    $where2['parent_id'] = $v1['id'];
                    $where2['status'] = 1;

                    if($privilege_value!="*") {
                        $where2['id'] = ['in', $privilege_value];
                    }

                    $click1 = Db::name("privilege")->where($where2)->select();
                    foreach ($click1 as $k2=>$v2){
                        $where3['parent_id'] = $v2['id'];
                        $where3['status'] = 1;

                        if($privilege_value!="*") {
                            $where3['id'] = ['in', $privilege_value];
                        }

                        $click2 = Db::name("privilege")->where($where3)->select();
                        $click1[$k2]['click'] = $click2;
                    }
                    $click[$k1]['click'] = $click1;
                }
                $privilege[$k]['click'] = $click;
            }
            $assign['privilege'] = $privilege;
            $this->assign($assign);
            return view("addView");
        }else{
            $role = Db::name("role")->where(['id'=>$_GET['id']])->find();
            $privilege_list = explode(",",$role['privilege_list']);

            //获取一级权限组
            $where['parent_id'] = 0;
            $where['status'] = 1;
            if($privilege_value!="*") {
                $where['id'] = ['in', $privilege_value];
            }
            $privilege = Db::name("privilege")->where($where)->select();

            //查询二级
            foreach ($privilege as $k=>$v){
                $where1['parent_id'] = $v['id'];
                $where1['status'] = 1;
                if($privilege_value!="*") {
                    $where1['id'] = ['in', $privilege_value];
                }

                if(in_array($v['id'],$privilege_list)){
                    $privilege[$k]['selected'] = 1;
                }else{
                    $privilege[$k]['selected'] = 0;
                }

                $click = Db::name("privilege")->where($where1)->select();
                foreach ($click as $k1=>$v1){
                    $where2['parent_id'] = $v1['id'];
                    $where2['status'] = 1;

                    if($privilege_value!="*") {
                        $where2['id'] = ['in', $privilege_value];
                    }

                    $click1 = Db::name("privilege")->where($where2)->select();

                    foreach ($click1 as $k2=>$v2){
                        $where3['parent_id'] = $v2['id'];
                        $where3['status'] = 1;

                        if($privilege_value!="*") {
                            $where3['id'] = ['in', $privilege_value];
                        }

                        $click2 = Db::name("privilege")->where($where3)->select();

                        foreach ($click2 as $k3=>$v3){
                            if(in_array($v3['id'],$privilege_list)){
                                $click2[$k3]['selected'] = 1;
                            }else{
                                $click2[$k3]['selected'] = 0;
                            }

                            foreach ($click2 as $k4=>$v4){
                                if(in_array($v4['id'],$privilege_list)){
                                    $click2[$k4]['selected'] = 1;
                                }else{
                                    $click2[$k4]['selected'] = 0;
                                }
                            }

                        }
                        $click1[$k2]['click'] = $click2;
                    }

                    if(in_array($v1['id'],$privilege_list)){
                        $click[$k1]['selected'] = 1;
                    }else{
                        $click[$k1]['selected'] = 0;
                    }

                    foreach ($click1 as $k2=>$v2){
                        if(in_array($v2['id'],$privilege_list)){
                            $click1[$k2]['selected'] = 1;
                        }else{
                            $click1[$k2]['selected'] = 0;
                        }
                    }
                    $click[$k1]['click'] = $click1;
                }
                $privilege[$k]['click'] = $click;
            }
            $assign['privilege'] = $privilege;
            $assign['role'] = $role;
            $this->assign($assign);
            return view("saveView");
        }


    }

    /**
     * 添加角色
     */
    public function add()
    {
        $rule = [
            ["privilege_list", "require", "请选择权限"],
            ["name", "require|unique:Role", "请输入角色名称|角色名称重复,请重写"]
        ];
        $validate = new Validate($rule);
        $data = $this->request->post();
        if (!$validate->check($data)) {
            $this->msg($validate->getError(), "添加角色", "error");
        }
        $data["privilege_list"] = implode(",", $data["privilege_list"]);
        if (!\app\index\model\Role::create($data)) {
            $this->msg("添加失败", "添加角色", "error");
        }else{
            $this->msg("添加成功", "添加角色", "success");
        }
        $this->successSessionMsg("添加角色成功");
    }


    /**
     * 角色修改
     */
    public function save()
    {
        $rule = [
            ["privilege_list", "require", "请选择权限"],
            ["name", "require|unique:Role", "请输入角色名称|角色名称重复,请重写"]
        ];
        $validate = new Validate($rule);
        $data = $this->request->post();
        if (!$validate->check($data)) {
            $this->msg($validate->getError(), "修改角色", "error");
        }
        $data["privilege_list"] = implode(",", $data["privilege_list"]);
        if (!\app\index\model\Role::update($data)) {
            $this->msg("修改失败", "修改角色", "error");
        }
        $this->successSessionMsg("修改角色成功");
    }

    /**
     * 删除操作
     */
    public function del()
    {
        $id = $this->request->post("id");
        if(!\app\index\model\Role::destroy($id)){
            $this->msg("删除失败", "删除操作","error");
        }
        $this->successSessionMsg("删除成功");
    }
}